Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

369
Views
How to compile Kotlin code to jvmTarget="1.8" for tests

In our Android project we code in Kotlin and target java 1.6. However we are forced to java 1.8 in our test since some JUnit5 features requires it (static methods in interfaces).

Is it possibe to compile the tests differently than the production code?

We tried to raise the jvmTarget to 1.8 by adding this to our build.gradle:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            jvmTarget = "1.8"
        }
}

This raises the jvmTarget of the production code too, but we only want it for our tests. The Docs indicate that it can be specified for the test like this:

compileReleaseUnitTestKotlin{
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Unfortunatly the build.gradle doesn't compile.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The Kotlin docs mention how to do it for tests only.

Groovy DSL:

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

Kotlin DSL:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val compileTestKotlin: KotlinCompile by tasks

compileTestKotlin.kotlinOptions.jvmTarget = "1.8"
over 4 years ago · Santiago Trujillo Report

0

This snippet does the job for me:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    if (it.name.contains("UnitTest")) {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
}

I have to say, It's not the best solution to filter the test tasks based on their names, so think of this as a quick and dirty aid.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!